package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class CalculatorActivity_teacher extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_result;

    //第一个操作数
    private String firstNum = "";

    //运算符
    private String operator = "";
    //第二个操作数
    private String secondNum = "";

    //当前的计算结果
    private String result = "";

    //显示的文本内容
    private String showText = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator_teacher);

        //从布局文件中获取名为tv_result的文本视图
        tv_result = findViewById(R.id.tv_result);
        //下面给每个按钮控件都注册点击监听器
        int[] buttonIds = new int[]{R.id.btn_cancel, R.id.btn_divide, R.id.btn_multiply,
                R.id.btn_clear, R.id.btn_seven, R.id.btn_eight, R.id.btn_nine, R.id.btn_plus, R.id.btn_four,
                R.id.btn_five, R.id.btn_six, R.id.btn_minus, R.id.btn_one, R.id.btn_two, R.id.btn_three,
                R.id.btn_sqrt, R.id.btn_reciprocal, R.id.btn_zero, R.id.btn_dot, R.id.btn_equal};

        for (int id : buttonIds) {
            findViewById(id).setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        String inputText;
        //如果是开根号的按钮
        if (v.getId() == R.id.btn_sqrt) {
            inputText = "√";
        } else {
            inputText = ((TextView) v).getText().toString();
        }
        //点击清除按钮
        if (id == R.id.btn_clear) {
            clear();
        }
        //点击回退按钮
        else if (id == R.id.btn_cancel) {
            //把上一次的结果存起来
            refreshOperate(showText.substring(0,showText.length()-1));
            refreshText(showText.substring(0,showText.length()-1));
        }
        //点击了加、减、乘、除按钮
        else if (id == R.id.btn_plus || id == R.id.btn_minus || id == R.id.btn_multiply ||
                id == R.id.btn_divide) {
            operator = inputText; //运算符
            refreshText(showText + operator);
        }
        //点击了等号按钮
        else if (id == R.id.btn_equal) {
            //加减乘除四则运算
            double calculate_result = calculateFour();
            //把上一次的结果存起来
            refreshOperate(String.valueOf(calculate_result));
            //更新TextView的内容
            refreshText(showText + "=" + result);
        }
        //点击了开根号按钮
        else if (id == R.id.btn_sqrt) {
            double sqrt_result = Math.sqrt(Double.parseDouble(firstNum));
            //把上一次的结果存起来
            refreshOperate(String.valueOf(sqrt_result));
            //更新TextView的内容
            refreshText(showText + "√=" + result);
        }

        //点击了求倒数按钮
        else if (id == R.id.btn_reciprocal) {
            double reciprocal_result = 1.0 / Double.parseDouble(firstNum);
            //把上一次的结果存起来
            refreshOperate(String.valueOf(reciprocal_result));
            //更新TextView的内容
            refreshText(showText + "^-1=" + result);
        }
        //点击了其他按钮，包括数字和小数点
        else {
            //上次的运算结果已经出来了
            if(!result.isEmpty() && operator.isEmpty()){
                clear();
            }
            //无运算符，则继续拼接第一个操作数
            if (operator.isEmpty()) {
                firstNum = firstNum + inputText;
            } else {
                //有运算符，则继续拼接第二个操作数
                secondNum = secondNum + inputText;
            }
            //整数不需要前面的0
            if(showText.equals("0") && !inputText.equals(".")){
                refreshText(inputText);
            }
            refreshText(showText + inputText);
        }
    }

    //加减乘除四则运算
    private double calculateFour() {
        switch (operator){
            case "+":
                return Double.parseDouble(firstNum) + Double.parseDouble(secondNum);
            case "-":
                return Double.parseDouble(firstNum) - Double.parseDouble(secondNum);
            case "x":
                return Double.parseDouble(firstNum) * Double.parseDouble(secondNum);
            default:
                return Double.parseDouble(firstNum) / Double.parseDouble(secondNum);
        }
    }

    //清空并初始化
    private void clear() {
        refreshOperate("");
        refreshText("");
    }

    //刷新运算结果
    private void refreshOperate(String new_result){
        result = new_result;
        firstNum = result;
        secondNum = "";
        operator = "";
    }

    private void refreshText(String text) {
        showText = text;
        tv_result.setText(showText);
    }
}